﻿using System;
using System.Data;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.EBOOK.Components;

namespace ASPNETVN.PORTAL.Modules.EBOOK.Controls
{
    public partial class Book : ModulePage
    {
        private void GridDataBinding()
        {
            DataView dv;
            if (Request.QueryString["CategoryID"] != null)
            {
                // Begin build page link.
                int categoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);
                PageLink pagelink = (PageLink)this.ParentControl.FindControl("MainPageLink");
                Category category = Category.Load(categoryID);
                Category parentCategory = Category.Load(category.ParentID);

                pagelink.AddLink(parentCategory.Name, "#");
                string url = ResolveClientUrl(HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Book, "categoryid={0}", category.ID));

                pagelink.AddLink(category.Name, url);
                // End build page link.

                dv = Components.Book.SelectBy_CategoryID(Int32.Parse(Request.QueryString["categoryid"])).Tables[0].DefaultView;
            }
            else
            {
                if (Request.QueryString["OptionView"] != null)
                {
                    dv = Components.Book.GetTopItems(this.ModuleID, Request.QueryString["OptionView"]).Tables[0].DefaultView;
                }
                else
                {
                    dv = Components.Book.GetTopItems(this.ModuleID, string.Empty).Tables[0].DefaultView;
                }
            }
            gvItem.DataSource = dv;
            gvItem.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.GridDataBinding();
            }
        }

        protected void gvItem_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
        {
            gvItem.PageIndex = e.NewPageIndex;
            this.GridDataBinding();
        }
    }
}